package br.com.gerenciador_recursos.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import br.com.gerenciador_recursos.GerencidorRecursosBusinessException;
import br.com.gerenciador_recursos.NoSuchUserException;
import br.com.gerenciador_recursos.RequiredFieldException;
import br.com.gerenciador_recursos.TaskAlreadyExistNameException;
import br.com.gerenciador_recursos.model.User;
import br.com.gerenciador_recursos.model.UserType;

public class UserLocalServiceUtil {

	private static List<User> users;

	static {
		users = new ArrayList<User>();

		try {
			Set<UserType> userTypes = new HashSet<UserType>();
			userTypes.add(UserType.ADMINISTRADOR);

			add("Rafael Praxedes", "Rafael", "Gomes", "rafaprax28@gmail.com",
					"123456", userTypes);

			userTypes = new HashSet<UserType>();
			userTypes.add(UserType.DIRETOR);

			add("Diretor", "Diretor", "Nobrega", "diretor@teste.com.br",
					"123456", userTypes);

			userTypes = new HashSet<UserType>();
			userTypes.add(UserType.GERENTE);

			add("Gerente", "Gerente", "Carlos", "gerente@teste.com.br",
					"123456", userTypes);

			userTypes = new HashSet<UserType>();
			userTypes.add(UserType.DESENVOLVEDOR);

			add("Desenvolvedor 1", "Desenvolvedor", "1",
					"desenvolvedor1@teste.com.br", "123456", userTypes);

			userTypes = new HashSet<UserType>();
			userTypes.add(UserType.DESENVOLVEDOR);

			add("Desenvolvedor 2", "Desenvolvedor", "2",
					"desenvolvedor2@teste.com.br", "123456", userTypes);

		} catch (GerencidorRecursosBusinessException e) {
			e.printStackTrace();
		}

	}

	public static User add(String fullName, String firstName, String lastName,
			String emailAddress, String password, Set<UserType> userType)
			throws GerencidorRecursosBusinessException {

		validate(true, null, fullName, firstName, lastName, emailAddress,
				password, userType);

		User user = new User(IDGeneratorUtil.increment(), fullName, firstName,
				lastName, emailAddress, password, userType);

		users.add(user);

		return user;
	}

	public static User update(int userId, String fullName, String firstName,
			String lastName, String emailAddress, String password,
			Set<UserType> userType) throws GerencidorRecursosBusinessException {

		User user = findByPrimaryKey(userId);

		validate(false, user, fullName, firstName, lastName, emailAddress,
				password, userType);

		users.remove(user);

		user = new User(userId, fullName, firstName, lastName, emailAddress,
				password, userType);

		users.add(user);

		return user;
	}

	public static void delete(int userId) throws NoSuchUserException {
		User user = findByPrimaryKey(userId);
		users.remove(user);
	}

	public static User findByPrimaryKey(int userId) throws NoSuchUserException {

		User user = fetchByPrimaryKey(userId);

		if (user != null)
			return user;

		throw new NoSuchUserException();
	}

	public static User fetchByPrimaryKey(int userId) {

		for (User user : users) {
			if (user.getUserCode() == userId)
				return user;
		}

		return null;
	}

	public static User findByEmailAddress(String emailAddress) {

		for (User user : users) {
			if (user.getEmailAddress().trim().equals(emailAddress.trim()))
				return user;
		}

		return null;
	}

	public static User findByEmailAddressPassword(String name, String password) {

		User user = findByEmailAddress(name);

		if (user != null && user.getPassword().equals(password)) {
			return user;
		}

		return null;
	}

	private static void validate(boolean isNew, User oldUser, String fullName,
			String firstName, String lastName, String emailAddress,
			String password, Set<UserType> userType)
			throws GerencidorRecursosBusinessException {

		if (firstName == null || firstName.trim().equals(""))
			throw new RequiredFieldException();

		User user = findByEmailAddress(emailAddress);

		if (user != null && user.equals(oldUser)) {
			throw new TaskAlreadyExistNameException();
		}

		if (isNew) {

		} else {

		}

	}

	public static List<User> findAll() {

		return new ArrayList<User>(users);
	}

}
